//
//  SettingController.m
//  Conference
//
//  Created by 陈鑫 on 12-12-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SettingController.h"
#import "NetContant.h"
#import "Globals.h"
#import "WebData.h"
#import "Constant.h"
@interface SettingController ()

@end

@implementation SettingController
@synthesize btn_isSafe;
@synthesize isChecked;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isChecked = [[NSUserDefaults standardUserDefaults] boolForKey:[[Constant getInstance] IS_HIDE_MYSELF]];
    [self.btn_isSafe setSelected:self.isChecked];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setBtn_isSafe:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [btn_isSafe release];
    [super dealloc];
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];

}

- (IBAction)isSelectAction:(id)sender {
    
    [self.view addSubview:[[Globals getInstance] progress_view]];
    NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
    [thread start];
}
-(void)run{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSString *sCheck=@"false";
    if(self.isChecked)
     sCheck=@"true";
    NSString *sRet=[[WebData getInstance]updatePrivacy:sCheck];

    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:sRet waitUntilDone:YES];
    
	[pool release];
    
}
-(void)onPostExecute:(NSString *)sRet{
    
   [ [[Globals getInstance] progress_view] removeFromSuperview];
    
    if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
        
        
       [[Globals getInstance] showAlertView:[[Constant getInstance] net_exception]];
        
        
    }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_DB_EXCEPTION]]){
        
      [[Globals getInstance] showAlertView:[[Constant getInstance] net_exception]];
        
    }else {
        @try {
            [self.btn_isSafe setSelected:!self.btn_isSafe.isSelected];
            [[NSUserDefaults standardUserDefaults] setBool:self.btn_isSafe.isSelected forKey:[[Constant getInstance] IS_HIDE_MYSELF]]; 
              [[Globals getInstance] showAlertView:[[Constant getInstance] set_privacy_succ]];
        }
        @catch (NSException *exception) {
             [[Globals getInstance] showAlertView:[[Constant getInstance] net_exception]];
        }
        
        
    }
    
}

@end
